//
//  MyViewController.m
//  CoolFM
//
//  Created by 曹曹 on 16/8/30.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "MyViewController.h"
#import "LoginViewController.h"
#import "SettingViewController.h"
#import "CFMacro.h"
#import "UIView+Frame.h"
#import "MineTableViewCell.h"
#import "GXNetTool.h"
#import "DJLocationRoot.h"
#import "DJRootModel.h"
#import "RecentCollectionViewCell.h"
#import "FocusCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIView+Frame.h"

#define kSystemCellectionCellReuse @"sysCel"
#define kRecentCollectionCellReuse @"recent"
#define kFocussCollectionCellReuse @"focuss"

@interface MyViewController () <UICollectionViewDelegate, UICollectionViewDataSource, LoginDelegate>

// CollectionView
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
// layout
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
// 用户头像
@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
// 用户描述
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
// 用户名
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
// 绿色的线
@property (weak, nonatomic) IBOutlet UIView *lineView;
// 单独的按钮栏
@property (weak, nonatomic) IBOutlet UIView *menuView;
// 整个用户信息栏
@property (weak, nonatomic) IBOutlet UIView *userView;
//
@property (nonatomic, copy) NSString *sessionID;

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configCollectionView];
}

- (void)configCollectionView {
    CGFloat userViewH = 93;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goLogin)];
    [_userView addGestureRecognizer:tap];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    // 减去三个值得到item的高度
    _layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - userViewH - 49 - 64);
    
    
    
    NSLog(@"%lf", _layout.itemSize.height);
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing = 0;
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    // 注册cell
    [_collectionView registerNib:[UINib nibWithNibName:@"RecentCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kRecentCollectionCellReuse];
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kSystemCellectionCellReuse];
    [_collectionView registerNib:[UINib nibWithNibName:@"FocusCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kFocussCollectionCellReuse];
    
    // KVO
    [_collectionView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"mmm"];
}

#pragma mark - login
- (void)goLogin {
    LoginViewController *login = [[LoginViewController alloc] init];
    login.delegate = self;
    [self.navigationController pushViewController:login animated:YES];
}

#pragma mark - collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSystemCellectionCellReuse forIndexPath:indexPath];
    if (indexPath.row == 0) {
        RecentCollectionViewCell *recent = [collectionView dequeueReusableCellWithReuseIdentifier:kRecentCollectionCellReuse forIndexPath:indexPath];
        
        return recent;
    } else if (indexPath.row == 1) {
        FocusCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kFocussCollectionCellReuse forIndexPath:indexPath];
        cell.sessionID = _sessionID;
        return cell;
    } else if (indexPath.row == 2) {
        cell.backgroundColor = [UIColor orangeColor];
    } else {
        cell.backgroundColor = [UIColor purpleColor];
    }
    return cell;
}

- (IBAction)gotoRecent:(id)sender {
    [_collectionView setContentOffset:CGPointMake(0, 0) animated:NO];
    
}
- (IBAction)gotoFocus:(id)sender {
    [_collectionView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:NO];
    
}
- (IBAction)gotoDownload:(id)sender {
    [_collectionView setContentOffset:CGPointMake(SCREEN_WIDTH * 2, 0) animated:NO];
    
}
- (IBAction)gotoSonglist:(id)sender {
    [_collectionView setContentOffset:CGPointMake(SCREEN_WIDTH * 3, 0) animated:NO];
}

- (IBAction)goToSettingVC:(id)sender {
    SettingViewController *settingVC = [[SettingViewController alloc] init];
    settingVC.navigationItem.title = @"设置";
    [self.navigationController pushViewController:settingVC animated:YES];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    for (UIView *view in self.menuView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)view;
            [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:11];
        }
    }
    CGFloat newX = [[change objectForKey:@"new"] CGPointValue].x;
    NSInteger newTag = (newX + SCREEN_WIDTH / 2) / SCREEN_WIDTH;
    UIButton *button = [self.menuView viewWithTag:newTag + 10];
    [button setTitleColor:kDefaultColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
}

- (void)getSessionID:(NSString *)sessionID {
    _sessionID = sessionID;
}

#pragma mark - other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
